package regpang.myapplication22.image;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;

import regpang.myapplication22.R;

/**
 * @author xmqian
 * @date 2018/5/2 0002
 * @desc 在Activity中选择要处理的ImageView及对应的Bitmap，调用工具类中方法即可：
 */
public class ImageToolsActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private ImageView colorIv;
    private SeekBar sxBar, bhdBar, ldBar;
    private static int MIN_COLOR = 100;
    private static int MAX_COLOR = 255;
    private float sx, bhd, ld;
    private Bitmap bmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.color_main);
        initViews();
    }

    private void initViews() {
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image_big);
        colorIv = (ImageView) findViewById(R.id.color_iv);
        colorIv.setImageBitmap(bmp);
        sxBar = (SeekBar) findViewById(R.id.sx_seekbar);
        bhdBar = (SeekBar) findViewById(R.id.bhd_seekbar);
        ldBar = (SeekBar) findViewById(R.id.ld_seekbar);
        sxBar.setOnSeekBarChangeListener(this);
        sxBar.setMax(MAX_COLOR);// 设置最大值
        sxBar.setProgress(MIN_COLOR);// 设置初始值（当前值）
        bhdBar.setOnSeekBarChangeListener(this);
        bhdBar.setMax(MAX_COLOR);
        bhdBar.setProgress(MIN_COLOR);
        ldBar.setOnSeekBarChangeListener(this);
        ldBar.setMax(MAX_COLOR);
        ldBar.setProgress(MIN_COLOR);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.sx_seekbar:
                sx = (progress - MIN_COLOR) * 1.0f / MIN_COLOR * 180;
                break;
            case R.id.bhd_seekbar:
                bhd = progress * 1.0f / MIN_COLOR;
                break;
            case R.id.ld_seekbar:
                ld = progress * 1.0f / MIN_COLOR;
                break;
        }
        colorIv.setImageBitmap(ImageTools.getColorImage(bmp, sx, bhd, ld));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}
